"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 防篡改
Case Name   : 客户端认证超时时间设置
Create At   : 2022/3/11
Owner       : @daiguatutu
Description :
    1.设置参数并重启数据库enableSeparationOfDuty=on
    gs_guc set -N all -I all -c enableSeparationOfDuty=on;
    gs_om -t restart
    2.查看参数authentication_timeout的值
    show authentication_timeout;
    3.配置参数authentication_timeout的值分别为：0s,600s,601s,60.1s,2min
    gs_guc reload -N all -I all -c authentication_timeout=[参数值]
    4.查看参数authentication_timeout的值
    show authentication_timeout;
    5.清理环境
    gs_guc reload -N all -I all -c authentication_timeout=[默认参数值]
    gs_guc set -N all -I all -c authentication_timeout=[默认参数值];
    gs_om -t restart
Expect      :
    1.成功
    2.成功
    3.参数值在范围1~600之间的整数设置成功,不符合要求的值设置失败
    4.成功,值为2min
    5.清理环境成功
History     :
"""

import os
import re
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Security(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.default_value1 = self.common.show_param('enableSeparationOfDuty')
        self.default_value2 = self.common.show_param('authentication_timeout')

    def test_security(self):
        text = '-----step1：设置参数并重启数据库enableSeparationOfDuty=on; ' \
               'expect:成功-----'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            'enableSeparationOfDuty=on')
        self.assertTrue(mod_msg, '执行失败:' + text)
        result = self.pri_sh.restart_db_cluster()
        self.assertTrue(result, '执行失败' + text)

        text = '-----step2：配置参数authentication_timeout的值分别为：' \
               '0s,600s,601s,60.1s,2min expect:参数值600s/2min设置成功,其他值设置失败-----'
        self.log.info(text)
        values = ['0s', '600s', '601s', '60.1s', '2min']
        for value in values:
            res = self.pri_sh.execute_gsguc('reload', '',
                                            f'authentication_timeout={value}',
                                            get_detail=True)
            if value in ['600s', '2min']:
                self.assertIn(self.constant.GSGUC_SUCCESS_MSG, res,
                              '执行失败:' + text)
            elif value == '60.1s':
                self.assertIn('requires an integer value', res,
                              '执行失败:' + text)
            else:
                assert_msg = r'The value (.*)is outside the valid range ' \
                             r'for parameter'
                self.assertIsNotNone(re.search(assert_msg, res),
                                     '执行失败:' + text)

        text = '-----step3：查看参数authentication_timeout; expect:成功,值为1.44h-----'
        self.log.info(text)
        value1 = self.common.show_param('authentication_timeout')
        self.assertEqual('2min', value1, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step4：清理环境-----')
        text = '-----恢复参数authentication_timeout; expect:成功-----'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'authentication_timeout='
                                            f'{self.default_value2}')

        text2 = '-----恢复参数enableSeparationOfDuty并重启数据库 expect:成功-----'
        self.log.info(text2)
        mod_msg1 = self.pri_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f'enableSeparationOfDuty='
                                             f'{self.default_value1}')
        restart_res = self.pri_sh.restart_db_cluster()

        self.log.info('-----断言tearDown执行成功-----')
        self.assertTrue(mod_msg, '执行失败:' + text2)
        self.assertTrue(mod_msg1, '执行失败:' + text2)
        self.assertTrue(restart_res, '执行失败:' + text2)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
